package com.gxuwz.zhangjiahui.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxuwz.zhangjiahui.entity.Dep;
import com.gxuwz.zhangjiahui.entity.Leader;
import com.gxuwz.zhangjiahui.entity.Student;
import com.gxuwz.zhangjiahui.service.IDepService;
import com.gxuwz.zhangjiahui.service.ILeaderService;
import com.gxuwz.zhangjiahui.service.IStudentService;
import org.apache.ibatis.annotations.Param;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangjiahui
 * @since 2021-06-20
 */
@Controller
@RequestMapping("/leader")
public class LeaderController {

    @Autowired
    private ILeaderService iLeaderService;
    @Autowired
    private IDepService iDepService;


    /**
     * 查询全部信息
     * @param modelAndView
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    @Pointcut
    @ResponseBody
    @RequestMapping(value = "/findLeader")
    public ModelAndView findLeader(ModelAndView modelAndView, Page page, Integer pageNumber, Leader leader,
                                    @Param("leaderId") String leaderId,
                                    HttpServletRequest request, HttpServletResponse response) {
        if(request.getSession().getAttribute("nowResult") != null){
            if (!request.getSession().getAttribute("nowResult").equals("now")) {
                request.getSession().setAttribute("result", "");
            }
            request.getSession().setAttribute("nowResult", "");
        }
        // 可以通过 wrapper 进行筛选!!!
        QueryWrapper<Leader> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("leader_id");
        // 进行模糊查询!!!
        if(leaderId != null && leaderId != ""){
            wrapper.like("leader_id", leaderId);
            request.getSession().setAttribute("leaderId", leaderId);
        }else {
            request.getSession().setAttribute("leaderId", "");
        }
        // Current,页码 + Size,每页条数
        if(pageNumber == null){
            page.setCurrent(1);
        }else {
            page.setCurrent((long)pageNumber);
        }
        // 默认每页6行数据！
        page.setSize(6);
        // 调用分页查询方法！!
        IPage<Leader> leaderIPage = iLeaderService.selectLeaderPage(page, wrapper);
        // 存放page，内有当前页数
        modelAndView.addObject("page", page);
        System.out.println("总条数"+leaderIPage.getTotal());
        System.out.println("总页数"+leaderIPage.getPages());
        // 存放总页数
        modelAndView.addObject("pages", (int)leaderIPage.getPages());
        // 存放一个数组用来让foreach遍历
        int[] pagesList = new int[(int)leaderIPage.getPages()];
        for(int i=0; i< (int)leaderIPage.getPages(); i++){
            pagesList[i] = i+1;
        }
        modelAndView.addObject("pagesList", pagesList);
        modelAndView.addObject("numberPages", leaderIPage.getTotal());
        List<Leader> leaderList = leaderIPage.getRecords();
        System.out.println("leaderList = "+leaderList);
        modelAndView.addObject("leaderList", leaderList);

        // 查询全部部门
        List<Dep> depList = iDepService.findDepAll();
        modelAndView.addObject("depList", depList);

        modelAndView.setViewName("leader/leader_list");
        return modelAndView;
    }



    /**
     * 添加/修改 信息
     * @param leader
     * @return
     */
    @PostMapping(value = "/addEditLeader")
    public ModelAndView addEditLeader(Leader leader, HttpServletRequest request, ModelAndView modelAndView) {
        Leader leader1 = iLeaderService.findLeaderById(leader);
        System.out.println("leader = " + leader);
        System.out.println("leader1 = " + leader1);
        // 新增信息
        if(leader1 == null){
            System.out.println("进入新增");
            try {
                iLeaderService.addLeader(leader);
                request.getSession().setAttribute("result", "addTrue");
                request.getSession().setAttribute("nowResult", "now");
            }catch (Exception e){
                request.getSession().setAttribute("result", "addFalse");
                request.getSession().setAttribute("nowResult", "now");
            }
        }
        // 修改用户信息
        if(leader1 != null){
            System.out.println("进入修改");
            try {
                iLeaderService.updateLeaderById(leader);
                request.getSession().setAttribute("result", "editTrue");
                request.getSession().setAttribute("nowResult", "now");
            }catch (Exception e){
                request.getSession().setAttribute("result", "editFalse");
                request.getSession().setAttribute("nowResult", "now");
            }
        }
        modelAndView.setViewName("redirect:/leader/findLeader");
        return modelAndView;
    }



    /**
     * 删除信息
     * @param leaderId
     * @return
     */
    @GetMapping(value = "/deleteLeader")
    public ModelAndView deleteLeader(HttpServletRequest request, ModelAndView modelAndView,
                             @Param("leaderId") String leaderId) {
        Leader leader = new Leader();
        leader.setLeaderId(leaderId);
        try {
            iLeaderService.deleteLeaderById(leader);
            request.getSession().setAttribute("result", "deleteTrue");
            request.getSession().setAttribute("nowResult", "now");
        }catch (Exception e){

        }
        modelAndView.setViewName("redirect:/leader/findLeader");
        return modelAndView;
    }

}
